package kr.pe.johnkim.checkmileage.mobile.rest.service;

import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageMemberMileageLog;
import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageMileage;
import kr.pe.johnkim.checkmileage.mobile.rest.dao.CheckMileageMemberMileageLogDao;
import kr.pe.johnkim.checkmileage.mobile.rest.dao.CheckMileageMileageDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Check Mileage 마일리지 관련 Service 기능들을 구현하는 클래스 이다.
 *
 * @author : johnkim <cale9797@gmail.com / Bettersoft, Inc.>
 * @Date : 12. 7. 25
 */
public class CheckMileageMileageServiceImpl implements CheckMileageMileageService {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(CheckMileageMileageServiceImpl.class);

    @Autowired
    private CheckMileageMileageDao checkMileageMileageDao = null;

    @Autowired
    private CheckMileageMemberMileageLogDao checkMileageMemberMileageLogDao = null;

    /**
     * 기존에 등록된 마일리지가 있는지 확인 한다.
     *
     * @param checkMileageMileage
     * @return
     */
    @Override
    public CheckMileageMileage checkMileageExist(CheckMileageMileage checkMileageMileage) {
        logger.info("checkMileageExist");

        return checkMileageMileageDao.checkMileageExist(checkMileageMileage);
    }

    /**
     * 기존에 가맹점에 등록된 사용자의 마일리지를 가져 온다.
     *
     * @param checkMileageMileage
     * @return
     */
    @Override
    public CheckMileageMileage selectMemberMileage(CheckMileageMileage checkMileageMileage) {
        logger.info("selectMemberMileage");

        return checkMileageMileageDao.selectMemberMileage(checkMileageMileage);
    }

    /**
     * 마일리지를 등록 한다.
     *
     * 1. 마일리지 등록에 성공하면 로그도 등록한다.
     * 2. 같은 transaction 으로 묶는다.
     *
     * @param checkMileageMileage
     * @return
     */
    @Override
    public void registerMileage(CheckMileageMileage checkMileageMileage) throws Exception {
        logger.info("registerMileage");

        checkMileageMileageDao.registerMileage(checkMileageMileage);

        String checkMileageMileagesIdCheckMileageMileages = checkMileageMileageDao.selectMileageId(checkMileageMileage).getIdCheckMileageMileages();
        CheckMileageMemberMileageLog parameterMemberMileageLog = new CheckMileageMemberMileageLog();

        parameterMemberMileageLog.setCheckMileageId(checkMileageMileage.getCheckMileageMembersCheckMileageId());
        parameterMemberMileageLog.setMerchantId(checkMileageMileage.getCheckMileageMerchantsMerchantId());
        parameterMemberMileageLog.setContent(checkMileageMileage.getContent());
        parameterMemberMileageLog.setMileage(checkMileageMileage.getMileage());
        parameterMemberMileageLog.setActivateYn(checkMileageMileage.getActivateYn());
        parameterMemberMileageLog.setModifyDate(checkMileageMileage.getModifyDate());
        parameterMemberMileageLog.setRegisterDate(checkMileageMileage.getRegisterDate());
        parameterMemberMileageLog.setCheckMileageMileagesIdCheckMileageMileages(checkMileageMileagesIdCheckMileageMileages);

        logger.info("checkMileageId                             : {}", parameterMemberMileageLog.getCheckMileageId());
        logger.info("merchantId                                 : {}", parameterMemberMileageLog.getMerchantId());
        logger.info("content                                    : {}", parameterMemberMileageLog.getContent());
        logger.info("mileage                                    : {}", parameterMemberMileageLog.getMileage());
        logger.info("activateYn                                 : {}", parameterMemberMileageLog.getActivateYn());
        logger.info("modifyDate                                 : {}", parameterMemberMileageLog.getModifyDate());
        logger.info("registerDate                               : {}", parameterMemberMileageLog.getRegisterDate());
        logger.info("checkMileageMileagesIdCheckMileageMileages : {}", parameterMemberMileageLog.getCheckMileageMileagesIdCheckMileageMileages());

        checkMileageMemberMileageLogDao.registerLog(parameterMemberMileageLog);
    }

    /**
     * 마일리지 점수를 업데이트 한다.
     *
     * 1. 마일리지 업데이트에 성공하면 로그도 등록한다.
     * 2. 같은 transaction 으로 묶는다.
     *
     * @param checkMileageMileage
     * @return
     */
    @Override
    public void updateMileage(CheckMileageMileage checkMileageMileage) throws Exception {
        logger.info("updateMileage");

        checkMileageMileageDao.updateMileage(checkMileageMileage);

        String checkMileageMileagesIdCheckMileageMileages = checkMileageMileageDao.selectMileageId(checkMileageMileage).getIdCheckMileageMileages();
        CheckMileageMemberMileageLog parameterMemberMileageLog = new CheckMileageMemberMileageLog();

        parameterMemberMileageLog.setCheckMileageId(checkMileageMileage.getCheckMileageMembersCheckMileageId());
        parameterMemberMileageLog.setMerchantId(checkMileageMileage.getCheckMileageMerchantsMerchantId());
        parameterMemberMileageLog.setContent(checkMileageMileage.getContent());

        if(("AMASS").equals(parameterMemberMileageLog.getContent())) {
            parameterMemberMileageLog.setMileage(checkMileageMileage.getAddMileage());
        } else {
            parameterMemberMileageLog.setMileage(checkMileageMileage.getSpendMileage());
        }

        parameterMemberMileageLog.setActivateYn(checkMileageMileage.getActivateYn());
        parameterMemberMileageLog.setModifyDate(checkMileageMileage.getModifyDate());
        parameterMemberMileageLog.setRegisterDate(checkMileageMileage.getRegisterDate());
        parameterMemberMileageLog.setCheckMileageMileagesIdCheckMileageMileages(checkMileageMileagesIdCheckMileageMileages);

        logger.info("checkMileageId                             : {}", parameterMemberMileageLog.getCheckMileageId());
        logger.info("merchantId                                 : {}", parameterMemberMileageLog.getMerchantId());
        logger.info("content                                    : {}", parameterMemberMileageLog.getContent());
        logger.info("mileage                                    : {}", parameterMemberMileageLog.getMileage());
        logger.info("activateYn                                 : {}", parameterMemberMileageLog.getActivateYn());
        logger.info("modifyDate                                 : {}", parameterMemberMileageLog.getModifyDate());
        logger.info("registerDate                               : {}", parameterMemberMileageLog.getRegisterDate());
        logger.info("checkMileageMileagesIdCheckMileageMileages : {}", parameterMemberMileageLog.getCheckMileageMileagesIdCheckMileageMileages());

        checkMileageMemberMileageLogDao.registerLog(parameterMemberMileageLog);
    }

    /**
     * 사용자의 마일리지 목록을 가져 온다. 사용자 smart phone 에 출력.
     *
     * @param checkMileageMileage
     * @return
     */
    @Override
    public List<CheckMileageMileage> selectMemberMerchantMileageList(CheckMileageMileage checkMileageMileage) {
        logger.info("selectMemberMerchantMileageList");

        return checkMileageMileageDao.selectMemberMerchantMileageList(checkMileageMileage);
    }

    /**
     * 가맹점 고객의 마일리지 목록을 가져 온다. 가맹점 tablet PC/iPad 에 출력.
     *
     * @param checkMileageMileage
     * @return
     */
    @Override
    public List<CheckMileageMileage> selectMerchantMemberMileageList(CheckMileageMileage checkMileageMileage) {
        logger.info("selectMerchantMemberMileageList");

        return checkMileageMileageDao.selectMerchantMemberMileageList(checkMileageMileage);
    }

    /**
     * 마일리지를 비활성화 한다. 사용자가 서비스 탈퇴 시 관련 정보 비활성화.
     *
     * @param checkMileageMileage
     */
    @Override
    public void updateDeactivateMileage(CheckMileageMileage checkMileageMileage) {
        logger.info("updateDeactivateMileage");

        checkMileageMileageDao.updateDeactivateMileage(checkMileageMileage);
    }

    /**
     * 사용자의 가장 최근에 등록된 마일리지 아이디를 가져 온다.
     *
     * @param checkMileageMileage
     * @return
     */
    @Override
    public CheckMileageMileage selectMileageId(CheckMileageMileage checkMileageMileage) {
        logger.info("selectMileageId");

        return checkMileageMileageDao.selectMileageId(checkMileageMileage);
    }
}
